package com.neo.test.iostream;

import org.junit.Test;

import java.io.File;

/**
 * @ClassName: FileDemo
 * @Title: demo
 * @Package: com.neo.test.iostream
 * @Description:
 * @Author: Kisen
 * @Date: 2021/8/22 23:01
 */
public class FileDemo {

    @Test
    public void testFileConstructor() {
        // pathname
        File liuBei = new File("D:/三国/刘备.png");
        // String parent, String child
        File guanYu = new File("D:/三国", "关羽.jpg");
        // 目录
        File sanGuo = new File("D:/三国");
        // File parent, String child
        File zhangFei = new File(sanGuo, "张飞.txt");
        // 可以声明不存在的文件
        File zhuGeLiang = new File(sanGuo, "诸葛亮.txt");
    }

    @Test
    public void testAbsolutePath() {
        File f = new File("D:/bbb.java");
        // D:\bbb.java
        System.out.println(f.getAbsolutePath());
        File f2 = new File("bbb.java");
        // E:\IntelliJ IDEA\Non-Project\demo\bbb.java
        System.out.println(f2.getAbsolutePath());
    }

    @Test
    public void testPathSeparator() {
        System.out.println(File.separator);
    }

    @Test
    public void testCreateAndDelFile() {
        File shuiHu = new File("D:/四大名著/水浒传");
        // 返回false 创建失败
        boolean mkdir = shuiHu.mkdir();
        System.out.println(mkdir);
        // 返回true 创建成功
        boolean mkdirs = shuiHu.mkdirs();
        System.out.println(mkdirs);

        File four = new File("D:/四大名著");
        // 返回false 删除目录时必须目录为空才能删除成功
        boolean delete = four.delete();
        System.out.println(delete);

        // File shuiHu = new File("D:/四大名著/水浒传");
        // true 正确删除了水浒传目录
        boolean delete1 = shuiHu.delete();
        System.out.println(delete1);

        File liuBei = new File("D:/三国/刘备.jpg");
        // 返回true 正确删除了刘备.jpg文件
        boolean delete2 = liuBei.delete();
        System.out.println(delete2);
    }

    @Test
    public void testIsDirectory() {
        File xiYou = new File("D:/西游记");
        // 文件或目录不存在时 返回false
        System.out.println(xiYou.isDirectory());
    }

    @Test
    public void testGetFile() {
        File shuiHu = new File("D:/水浒传");
        // 0
        System.out.println(shuiHu.length());
        File liuBei = new File("D:/三国/刘备.jpg");
        // 12963
        System.out.println(liuBei.length());

        File f = new File("D:/bbb.java");
        // D:\bbb.java
        System.out.println(f.getPath());

        File f2 = new File("bbb.java");
        // bbb.java
        System.out.println(f2.getPath());

        File sanGuo2 = new File("D:/三国2");
        // 该目录不存在，返回null
        String[] list = sanGuo2.list();
        System.out.println(list);
    }
}
